<?php 
	session_start();
	$var1= $_SESSION["registrado"];
	$var2= $_SESSION["administrador"];
	if ($var1 == null): {
		echo ("User not registered");
	} elseif ($var2 == 0): { 
			echo ("User without permissions");
	} else : {
?>


<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">

<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="">

<link href="css/bootstrap.css" rel="stylesheet" media="screen">
<link href="css/bootstrap-responsive.css" rel="stylesheet"	media="screen">

<title>My project manager</title>



</head>

<body>
<?php include ('configuration.php'); ?>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/bootstrap.js"></script>
    
<div class="span12">
<div class="navbar navbar-inverse navbar-fixed-top">
	<div class="navbar-inner">
		<div class="container-fluid">
			<button type="button" class="btn btn-navbar" data-toggle="collapse"
				data-target=".nav-collapse">
				<span class="icon-bar"></span> <span class="icon-bar"></span> <span
					class="icon-bar"></span>
			</button>
			<a class="brand"href="http://localhost/bases--grupo10/homeAdmin.php?">My project manager</a>
			<div class="nav-collapse collapse">
				<p class="navbar-text pull-right">
				
					<a href="http://localhost/bases--grupo10/logout.php?" class="navbar-link">Log out</a>
				</p>
				<ul class="nav">
					<li class="active"><a href="http://localhost/bases--grupo10/homeAdmin.php?">Home</a></li>
					<li><a href="http://localhost/bases--grupo10/about.php?">About</a></li>
					<li><a href="http://localhost/bases--grupo10/contact.php?">Contact</a></li>
				</ul>
			</div>
		</div>
	</div>
</div>
</div>


	<?php
		$name=$_POST['name_project'];
		$deleted=$_POST['deleted'];
		$advantage=$_POST['advantage'];
		$initial_date=$_POST['fecha'];
		$final_date=$_POST['fecha2'];
		$id_version=$_POST['id_ver'];
		$id_project=$_POST['id_pro'];    
		$no_actualizar=0;		
        
		//consulto el avance almacenado en la base de datos
    	$q = mysql_query("SELECT p.advantage FROM project p WHERE p.name_project = '$name' AND p.id_version = '$id_version'");
   		$result1 = mysql_fetch_assoc($q);
   		$oldAdvantage = $result1['advantage'];
   	      
   		//consulto la fecha de fin almacenada en la base de datos
    	$query = mysql_query("SELECT p.final_date FROM project p WHERE p.name_project = '$name' AND p.id_version = '$id_version'");
   		$res = mysql_fetch_assoc($query);
   		$oldFinalDate = $res['final_date'];
   		
   		//consulto la fecha de inicio almacenada en la base de datos
    	$query1 = mysql_query("SELECT p.initial_date FROM project p WHERE p.name_project = '$name' AND p.id_version = '$id_version'");
		$res1 = mysql_fetch_assoc($query1);
		$oldInitialDate = $res1['initial_date'];
    	
   		//consulto el nombre en la base de datos
		$query2 = mysql_query("SELECT p.name_project FROM project p WHERE p.name_project = '$name' AND p.id_version = '$id_version'");
   		$res2 = mysql_fetch_assoc($query2);
   		$oldNameProject = $res2['name_project'];


   		//POSIBLES ERRORES
   		//si el avance es 100% no puede modificarse porque el proyecto ya ha terminado
   		if ($oldAdvantage == 100) {
   			$no_actualizar=1;
			echo "<h1 style='position:absolute;top:100px;left:50px;font-weight:bold'><font size='5'><u> The project has finished because it has the 100% in the advance and it can't be modified</u></font></h1>"; 
			echo "<a class='btn btn-primary' type='button' href='http://localhost/bases--grupo10/modifyProject2_restore.php?' style='position:absolute;top:250px;left:50px'>Back</a>";
   		} else {
   			//si el avance a�n no es 100%
	   		//se ha modificado el nombre del proyecto
    		$query_name=mysql_query("SELECT id_project FROM project WHERE name_project='$name'") or die ("Query error");
    		$result_query = mysql_fetch_assoc($query_name);
   			$idNewName = $result_query['id_project'];   			
    		if ($idNewName != null && $idNewName != $id_project) {
    			//el nombre de projecto introducido ya existe
       			$no_actualizar=1;
				echo "<h1 style='position:absolute;top:100px;left:50px;font-weight:bold'><font size='5'><u> This project's name exists, please choose another one</u></font></h1>"; 
				echo "<a class='btn btn-primary' type='button' href='http://localhost/bases--grupo10/modifyProject2_restore.php?' style='position:absolute;top:250px;left:50px'>Back</a>";
   			} elseif ($name == null){
   				//no puede ser nulo
	   			$no_actualizar=1;
				echo "<h1 style='position:absolute;top:100px;left:50px;font-weight:bold'><font size='5'><u> The project's name can't be null</u></font></h1>"; 
				echo "<a class='btn btn-primary' type='button' href='http://localhost/bases--grupo10/modifyProject2_restore.php?' style='position:absolute;top:250px;left:50px'>Back</a>";
	   		} elseif ($initial_date == $oldInitialDate && $final_date == $oldFinalDate && $advantage == $oldAdvantage && (($idNewName != null && $idNewName == $id_project)) ) {
	   			//si no se ha introducido ninguna modificaci�n se informa y no se inserta en la base de datos
   				$no_actualizar=1;
	   			echo "<h1 style='position:absolute;top:100px;left:50px;font-weight:bold'><font size='5'><u> You have not introduced any modification</u></font></h1>"; 
				echo "<a class='btn btn-primary' type='button' href='http://localhost/bases--grupo10/modifyProject2_restore.php?' style='position:absolute;top:250px;left:50px'>Back</a>";
	   		}
	   		//las fechas no pueden ser nulas
	   		if ($initial_date == null || $final_date == null) {
	   			$no_actualizar=1;
				echo "<h1 style='position:absolute;top:130px;left:50px;font-weight:bold'><font size='5'><u> Dates can't be null</u></font></h1>"; 
				echo "<a class='btn btn-primary' type='button' href='http://localhost/bases--grupo10/modifyProject2_restore.php?' style='position:absolute;top:250px;left:50px'>Back</a>";	
	   		}
	   		//se ha modificado la fecha de inicio 
			if ($oldAdvantage != 0 && $oldInitialDate != $initial_date && $initial_date != null) {
				$no_actualizar=1;
				echo "<h1 style='position:absolute;top:130px;left:50px;font-weight:bold'><font size='5'><u> Initial date can't be modified because the advantage is not null</u></font></h1>"; 
				echo "<a class='btn btn-primary' type='button' href='http://localhost/bases--grupo10/modifyProject2_restore.php?' style='position:absolute;top:250px;left:50px'>Back</a>";
			}
			//se ha modificado el avance
			if ($oldAdvantage != $advantage) {
	   			if ($advantage < 0 || $advantage > 100) {
					//el avance debe ser un numero positivo comprendido entre 0 y 100
					$no_actualizar=1;
					echo "<h1 style='position:absolute;top:160px;left:50px;font-weight:bold'><font size='5'><u>The advance must be a positive number between 0 and 100</u></font></h1>"; 
					echo "<a class='btn btn-primary' type='button' href='http://localhost/bases--grupo10/modifyProject2_restore.php?' style='position:absolute;top:250px;left:50px'>Back</a>";		
	   			} elseif ($oldAdvantage > 0 && $advantage == 0) {
					//una vez que el avance sea mayor que 0, no puede volver a ser cero pero s� puede ser menos que el avance anterior
					$no_actualizar=1;
					echo "<h1 style='position:absolute;top:160px;left:50px;font-weight:bold'><font size='5'><u>Once the advance is bigger than zero can not return to zero</u></font></h1>"; 
					echo "<a class='btn btn-primary' type='button' href='http://localhost/bases--grupo10/modifyProject2_restore.php?' style='position:absolute;top:250px;left:50px'>Back</a>";
	   			}
			}
	   		//se ha modificado la fecha de fin
			if ($oldFinalDate!= $final_date &$final_date != null) {
				$anyoIni = substr($initial_date, 0, 4);
				$anyoFin = substr($final_date, 0, 4);
				$aux1 = substr($initial_date, 5, 6);
				$mesIni = substr($aux1, 0, 2);
				$aux2 = substr($final_date, 5, 6);
				$mesFin = substr($aux2, 0, 2);
				$diaIni = substr($initial_date, 8, 9);
				$diaFin = substr($final_date, 8, 9);
				//si la fecha fin es mayor que la de inicio se actualiza
				if( !($anyoFin - $anyoIni >= 0) ) {
					$no_actualizar=1;
					echo "<h1 style='position:absolute;top:190px;left:50px;font-weight:bold'><font size='5'><u> Initial date can't be bigger than final date</u></font></h1>"; 
					echo "<a class='btn btn-primary' type='button' href='http://localhost/bases--grupo10/modifyProject2_restore.php?' style='position:absolute;top:250px;left:50px'>Back</a>";
				} elseif ( ($anyoFin - $anyoIni == 0) && !($mesFin - $mesIni >= 0) ) {
					$no_actualizar=1;
					echo "<h1 style='position:absolute;top:190px;left:50px;font-weight:bold'><font size='5'><u> Initial date can't be bigger than final date</u></font></h1>"; 
					echo "<a class='btn btn-primary' type='button' href='http://localhost/bases--grupo10/modifyProject2_restore.php?' style='position:absolute;top:250px;left:50px'>Back</a>";
				} elseif ( ($anyoFin - $anyoIni == 0) && ($mesFin - $mesIni == 0) && !($diaFin - $diaIni >=0) ) {
					$no_actualizar=1;
					echo "<h1 style='position:absolute;top:190px;left:50px;font-weight:bold'><font size='5'><u> Initial date can't be bigger than final date</u></font></h1>"; 
					echo "<a class='btn btn-primary' type='button' href='http://localhost/bases--grupo10/modifyProject2_restore.php?' style='position:absolute;top:250px;left:50px'>Back</a>";
				}
			}
   		}

		//SI TODO HA IDO BIEN SE INSERTA UNA NUEVA VERSION	
		if ($no_actualizar==0): {
			$q2 = mysql_query("INSERT INTO project (id_project, name_project, deleted, id_version, advantage, initial_date, final_date) VALUES ('$id_project', '$name', '$deleted', '$id_version'+1, '$advantage', '$initial_date', '$final_date')");
			echo "<h1 class='muted'  style='position:absolute;top:50px;left:50px;'>Project $name <u>changed</u> </h1>";
			?>
			<a class="btn btn-primary" type="button" href="http://localhost/bases--grupo10/modifyProject2_restore.php?" style="position:absolute;top:200px;left:200px">Modified more</a>
			<a class="btn btn-primary" type="button" href="http://localhost/bases--grupo10/homeAdmin.php?" style="position:absolute;top:250px;left:225px">Finish</a>
			<?php 
		} endif;
	
		?>
	
</body>
</html> 

<?php }endif;?>